﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PMQLCHH.BUS;
using PMQLCHH.DTO;

namespace PMQLCHH.GUI
{
    public partial class POPUPKHACHHANG : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void GetString(string makhachhang,string tenkhachhang,string dienthoai,string diachi,string masothue);
        public GetString get_MaKH; 

        public POPUPKHACHHANG()
        {
            InitializeComponent();
        }

        private void POPUPKHACHHANG_Load(object sender, EventArgs e)
        {
            NapDuLieu();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_timkiem_Click(object sender, EventArgs e)
        {

        }

        //Quang Dat Them PopUp ngay 08/06/2013
        #region Quang Dat Them PopUp ngay 08/06/2013

        public void NapDuLieu()
        {
            this.txt_timkiem.Select();
            toolTip1.SetToolTip(this.txt_timkiem, "Điền vào thông tin tìm kiếm");

            //Load loai khach hang
            DataTable dt_timkiem = KhachHangBUS.LoadListLoaiKhachHang();
            if (dt_timkiem.Rows.Count > 0)
            {

                DataTable dt_kh_add = new DataTable();
                dt_kh_add.Columns.Add("MaLoaiKhachHang");
                dt_kh_add.Columns.Add("TenLoaiKhachHang");


                for (int i = 0; i < dt_timkiem.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        DataRow row1 = dt_kh_add.NewRow();
                        row1["MaLoaiKhachHang"] = "0";
                        row1["TenLoaiKhachHang"] = "Tất Cả Loại Khách Hàng";
                        dt_kh_add.Rows.Add(row1);
                    }

                    DataRow row = dt_kh_add.NewRow();
                    row["MaLoaiKhachHang"] = dt_timkiem.Rows[i]["MaLoaiKhachHang"].ToString();
                    row["TenLoaiKhachHang"] = dt_timkiem.Rows[i]["TenLoaiKhachHang"].ToString();
                    dt_kh_add.Rows.Add(row);
                }

                cbb_khachhang.DataSource = dt_kh_add;
                cbb_khachhang.ValueMember = "MaLoaiKhachHang";
                cbb_khachhang.DisplayMember = "TenLoaiKhachHang";
            }

            //Load DS DataGridView
            DataTable dt_kh = KhachHangBUS.LoadListKhachHang();

            if (dt_kh.Rows.Count > 0)
            {
                dtg_DanhSachKhachHang.AutoGenerateColumns = false;
                dtg_DanhSachKhachHang.DataSource = dt_kh;
            }
        }

        private void dtg_DanhSachKhachHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                DataGridViewCheckBoxCell chk_chon1 = dtg_DanhSachKhachHang.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(chk_chon1.Value) == false)
                {
                    chk_chon1.Value = null;
                }
            }

            if (e.ColumnIndex == 0)
            {
                //khoi tao lai gan tat ca bang null
                foreach (DataGridViewRow row in dtg_DanhSachKhachHang.Rows)
                {
                    DataGridViewCheckBoxCell chk_chon2 = row.Cells[0] as DataGridViewCheckBoxCell;
                    chk_chon2.Value = null;
                }

                //chon dong dang check
                DataGridViewCheckBoxCell chk_chon1 = dtg_DanhSachKhachHang.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                if (Convert.ToBoolean(chk_chon1.Value) == false)
                {
                    chk_chon1.Value = true;
                }
                else
                {
                    chk_chon1.Value = null;
                }
            }

        }

        private void dtg_DanhSachKhachHang_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == this.dtg_DanhSachKhachHang.Rows[0].Cells[0].RowIndex && e.RowIndex >= 0)
            {
                e.PaintBackground(e.ClipBounds, true);
                Rectangle rectRadioButton = default(Rectangle);
                rectRadioButton.Width = 12;
                rectRadioButton.Height = 12;
                rectRadioButton.X = e.CellBounds.X + (e.CellBounds.Width - rectRadioButton.Width) / 2;
                rectRadioButton.Y = e.CellBounds.Y + (e.CellBounds.Height - rectRadioButton.Height) / 2;
               
                if (e.Value is DBNull || e.Value == null)
                {
                    ControlPaint.DrawRadioButton(e.Graphics, rectRadioButton, ButtonState.Normal);
                }
                else
                {
                    ControlPaint.DrawRadioButton(e.Graphics, rectRadioButton, ButtonState.Checked);

                }
                e.Paint(e.ClipBounds, DataGridViewPaintParts.Focus);
                e.Handled = true;
            }
        }

        private void btn_hoantat_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dtg_DanhSachKhachHang.Rows)
            {
                DataGridViewCheckBoxCell chk_chon = row.Cells[0] as DataGridViewCheckBoxCell;

                if (Convert.ToBoolean(chk_chon.Value) == true)
                {
                    get_MaKH(row.Cells[1].Value.ToString(),row.Cells[2].Value.ToString(),row.Cells[4].Value.ToString(),row.Cells[3].Value.ToString(),row.Cells[8].Value.ToString());   
                }
            }
            this.Close();
        }

        #endregion
     

    }
}